import { Model } from 'dva';
import Service from '../../services/system/category';
import { ReduxAction } from '../global';

const model: Model = {
  namespace: 'category',
  state: {
    list: [],
  },
  subscriptions: {},
  reducers: {
    setState(state, action: ReduxAction) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  effects: {
    *list(_action, { put, call }) {
      const res = yield call(Service.list);
      yield put({ type: 'setState', payload: { list: res.data } });
    },

    *add(action, { call }) {
      return yield call(Service.add, action.payload);
    },

    *edit(action, { call }) {
      return yield call(Service.modifyById, action.payload);
    },

    *delete(action, { call }) {
      return yield call(Service.deleteById, action.payload);
    },
  },
};
export default model;
